home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / mplus_1.exe / GWDWDEMO.C < prev    next >
C/C++ Source or Header  |  1991-12-01  |  6KB  |  217 lines

  1. /*--------------------------------------------------------------
  2.  *  File:           GWDWDEMO.C
  3.  *  Description:    Program to demonstrate the window functions
  4.  *                  described in chapter 3 of the MPLUS Graphic
  5.  *                  Interface Library.
  6.  *
  7.  *  This demo developed for the MPLUS Graphic Interface Library.
  8.  *-------------------------------------------------------------*/
  9. #include <math.h>
  10. #include <graph.h>
  11. #include <stdio.h>
  12. #include "gscreen.h"
  13. #include "gplus.h"
  14.  
  15. /*--------------------------------------------------------------
  16.  *  Function Prototypes
  17.  *--------------------------------------------------------------*/
  18. void setaxis( GWDW *gwptr );
  19. void sinplot( double range1, double range2, short xpixels, int ysf );
  20. void cosplot( double range1, double range2, short xpixels, int ysf );
  21.  
  22. /*--------------------------------------------------------------
  23.  *  Function:       main
  24.  *  Description:    opens 3 windows, plots some sine and cosine
  25.  *                  waves, and demonstrates most of the MPLUS
  26.  *                  graphic window functions.
  27.  *  Return value:   0 returned to parent process.
  28.  *--------------------------------------------------------------*/
  29. main()
  30. {
  31.     int i;
  32.     char buffer[80];
  33.     short x1, y1, x2, y2;
  34.     word xo,yo;
  35.     double pi;
  36.     GWDW *groot;
  37.     GWDW *gwptr1;
  38.     GWDW *gwptr2;
  39.  
  40.     if( setvideomode( _ERESCOLOR ) == 0 )
  41.     {
  42.         printf("\nProgram requires EGA mode\n");
  43.         exit(1);
  44.     }
  45.     mpwordwrap(1);
  46.  
  47.     x1 = 50;
  48.     y1 = 20;
  49.     x2 = 550;
  50.     y2 = 340;
  51.     pi = 3.141592654;
  52.  
  53.     /*      Since window is large, conserve memory by using
  54.      *      grootopen().
  55.      */
  56.     groot = grootopen( x1, y1, x2, y2, _GBORDER, BLUE, WHITE );
  57.  
  58.     /*      Draw axis in black.  Set origin to center of window.
  59.      */
  60.     _setcolor( BLACK );
  61.     setaxis( groot );
  62.  
  63.     /*      Draw sine wave in red.
  64.      */
  65.     _setcolor( RED );
  66.     sinplot( -2*pi, 2*pi, x2-x1, 100 );
  67.     _settextposition( 20, 1 );
  68.     outtext( "y = sin(x)", RED, -1 );
  69.  
  70.     /*      Draw cosine wave in blue.
  71.      */
  72.     _setcolor( BLUE );
  73.     cosplot( -2*pi, 2*pi, x2-x1, 100 );
  74.     _settextposition( 21, 1 );
  75.     outtext( "y = cos(x)", BLUE, -1 );
  76.  
  77.     getch();
  78.  
  79.     gwptr1 = gwdwopen( 250, 100, 600, 300, _GBORDER, GREEN, BLACK );
  80.     _setcolor( LIGHTYELLOW );
  81.     setaxis( gwptr1 );
  82.  
  83.     gwptr2 = gwdwtopen( 5, 1, 15, 31, _GBORDER, MAGENTA, LIGHTYELLOW );
  84.     outtext( "Press a key to draw y=sin(x)\n", MAGENTA, -1 );
  85.     getch();
  86.  
  87.     gwdwsetactv( gwptr1 );
  88.     _setcolor( RED );
  89.     sinplot( -2*pi, 2*pi, 600-250, 50 );
  90.     _settextposition( 1, 1 );
  91.     outtext( "y = sin(x)", RED, -1 );
  92.  
  93.     gwdwsetactv( gwptr2 );
  94.     outtext( "Press a key to draw y=cos(x)\n", MAGENTA, -1 );
  95.     getch();
  96.  
  97.     gwdwsetactv( gwptr1 );
  98.     _setcolor( BLUE );
  99.     cosplot( -2*pi, 2*pi, 600-250, 50 );
  100.     _settextposition( 2, 1 );
  101.     outtext( "y = cos(x)", BLUE, -1 );
  102.  
  103.     gwdwsetactv( gwptr2 );
  104.     for( i=0; i<20; ++i )
  105.     {
  106.         sprintf(buffer, "\nThis is line %d", i );
  107.         outtext( buffer, MAGENTA, -1 );
  108.     }
  109.     outtext( "\nPress a key to clear this window...", MAGENTA, -1 );
  110.     getch();
  111.     gwdwclr( gwptr2 );
  112.     outtext( "Press a key to close window at right", MAGENTA, -1 );
  113.     getch();
  114.     gwdwclose( gwptr1 );
  115.     gwdwclr( gwptr2 );
  116.     outtext( "Press a key to close this window.", MAGENTA, -1 );
  117.     getch();
  118.     gwdwclose( gwptr2 );
  119.     getch();
  120.     grootclose( groot );
  121.     setvideomode( _DEFAULTMODE );
  122.     return 0;
  123. }
  124. /*--------------------------------------------------------------
  125.  *  Function:       sinplot
  126.  *  Description:    plot a sine wave
  127.  *  Return value:   none
  128.  *--------------------------------------------------------------*/
  129. void sinplot( range1, range2, xpixels, ysf )
  130. double range1;
  131. double range2;
  132. short xpixels;                  /* x pixels available */
  133. int ysf;                        /* y scale factor */
  134. {
  135.     int i;
  136.     double numperxpix;
  137.     double xpixpernum;
  138.     double x, y;
  139.  
  140.     numperxpix = (fabs(range2-range1))/(double)xpixels;
  141.     xpixpernum = 1/numperxpix;
  142.  
  143.     /*      Calculate first point.  Position cursor with _moveto.
  144.      *      Adjust sign for y axis.
  145.      */
  146.     x = range1;
  147.     y = sin( x );
  148.     _moveto( (short)(range1/numperxpix), (short)(-( y*ysf )) );
  149.  
  150.     for( i=1; i<xpixels; ++i)
  151.     {
  152.         x += numperxpix;
  153.         y = sin( x );
  154.         _lineto( (short)(x * xpixpernum), (short)(-(y*ysf)) );
  155.     }
  156. }
  157. /*--------------------------------------------------------------
  158.  *  Function:       cosplot
  159.  *  Description:    plot a cosine wave
  160.  *  Return value:   none
  161.  *--------------------------------------------------------------*/
  162. void cosplot( range1, range2, xpixels, ysf )
  163. double range1;
  164. double range2;
  165. short xpixels;                  /* x pixels available */
  166. int ysf;                        /* y scale factor */
  167. {
  168.     int i;
  169.     double numperxpix;
  170.     double xpixpernum;
  171.     double x, y;
  172.  
  173.     numperxpix = (fabs(range2-range1))/(double)xpixels;
  174.     xpixpernum = 1/numperxpix;
  175.  
  176.     /*      Calculate first point.  Position cursor with _moveto.
  177.      *      Adjust sign for y axis.
  178.      */
  179.     x = range1;
  180.     y = cos( x );
  181.     _moveto( (short)(range1/numperxpix), (short)(-(y*ysf)) );
  182.  
  183.     for( i=1; i<xpixels; ++i)
  184.     {
  185.         x += numperxpix;
  186.         y = cos( x );
  187.         _lineto( (short)(x*xpixpernum), (short)(-(y*ysf)) );
  188.     }
  189. }
  190. /*--------------------------------------------------------------
  191.  *  Function:       setaxis
  192.  *  Description:    draw axis and set logical origin to center
  193.  *                  of screen.
  194.  *  Return value:   none
  195.  *--------------------------------------------------------------*/
  196. void setaxis( gwptr )
  197. GWDW *gwptr;
  198. {
  199.     word x,y;
  200.     short xctr, yctr;
  201.  
  202.     xctr = (gwptr->x2-gwptr->x1)/2;
  203.     yctr = (gwptr->y2-gwptr->y1)/2;
  204.  
  205.     _moveto( xctr, 0 );
  206.     _lineto( xctr, gwptr->y2-gwptr->y1 );
  207.     _moveto( 0, yctr );
  208.     _lineto( gwptr->x2-gwptr->x1, yctr );
  209.  
  210.     gwdwsetorg( gwptr, xctr, yctr );
  211. }
  212. /*-------------------------------------------------------------*
  213.  *                  End of GWDWDEMO.C                          *
  214.  *-------------------------------------------------------------*/
  215.  
  216.  
  217.